package ai.people.netmon.framework.model.response;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.Collection;
import java.util.Collections;

/**
 * 分页数据
 *
 * @author yuanqinglong
 * @date 2022/1/12 16:04
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class PageResponse<T> {


    public PageResponse(Collection<T> data, long total) {
        this.list = data;
        this.total = total;
    }


    public PageResponse(Collection<T> data, long total, long pages, long current) {
        this.list = data;
        this.pages = pages;
        this.total = total;
        this.current = current;
    }

    /**
     * 数据
     */
    private Collection<T> list;
    /**
     * 总计
     */
    private Long total = 0L;

    /**
     * 页面
     */
    private Long pages = 0L;
    /**
     * 当前
     */
    private Long current = 1L;


    /**
     * 构建器
     *
     * @return {@link PageResponse}<{@link T}>
     */
    public static <T> PageResponse<T> builderEmpty() {
        return builder(Collections.emptyList(), 0);
    }

    /**
     * 构建器
     *
     * @param data  数据
     * @param total 总计
     * @return {@link PageResponse}<{@link T}>
     */
    public static <T> PageResponse<T> builder(Collection<T> data, long total) {
        return new PageResponse<>(data, total);
    }

    /**
     * 构建器
     *
     * @param data  数据
     * @param total 总计
     * @return {@link PageResponse}<{@link T}>
     */
    public static <T> PageResponse<T> builder(Collection<T> data, long total, long pages, long current) {
        return new PageResponse<>(data, total,pages, current);
    }

    /**
     * 构建器
     *
     * @return {@link PageResponse}<{@link T}>
     */
    public static <T> PageResponse<T> builder(Collection<T> data) {
        return new PageResponse<>(data, 0L, 0L, 0L);
    }

    /**
     * 构建器
     *
     * @return {@link PageResponse}<{@link T}>
     */
    public static <T> PageResponse<T> builder(Page<T> page) {
        return new PageResponse<>(page.getRecords(), page.getTotal(), page.getPages(), page.getCurrent());
    }

    /**
     * 构建器
     *
     * @return {@link PageResponse}<{@link T}>
     */
    public static <T> PageResponse<T> builder(IPage<T> page) {
        return new PageResponse<>(page.getRecords(), page.getTotal(), page.getPages(), page.getCurrent());
    }

}
